﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.ServiceOrderModules
{
    public class ServiceOrderDetailUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<ServiceOrderDetailModel> GetServiceOrderDetailUpdater()
        {
            NormalDataUpdater<ServiceOrderDetailModel> systemDataUpdater = new NormalDataUpdater<ServiceOrderDetailModel>();
            systemDataUpdater.DataUpdating += DeliveryDetailValid;
            systemDataUpdater.DataUpdating += ProductionValid;
            systemDataUpdater.DataUpdating += ServiceValid;
            systemDataUpdater.DataUpdating += StateValid ;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<ServiceOrderDetailModel> e)
        {
            if (e.Delete.State!=Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.AutoId);
            }
        }

        private void ServiceValid(object sender, DataValidationEventArgs<ServiceOrderDetailModel> e)
        {
            string message;
            ServiceOrderFetcher serviceOrderFetcher = new ServiceOrderFetcher();
            if(serviceOrderFetcher.Get(e.Insert.Site,e.Insert.ServiceId,out message)==null)
            {
                e.Message = "主档不存在";
                e.Cancle = true;
            }
            
        }
        private void StateValid(object sender, DataValidationEventArgs<ServiceOrderDetailModel> e)
        {
            string message;
            ServiceOrderFetcher serviceOrderFetcher = new ServiceOrderFetcher();
            ServiceOrderModel model = serviceOrderFetcher.Get(e.Insert.Site, e.Insert.ServiceId, out message);
            if (e.Insert.AutoId == 0 && model != null && model.State != Definition.STATE_NORMAL)
            {
                e.Message = "已审核的表单不能新增";
                e.Cancle = true;
                return;
            }
            if (e.Insert.State != Definition.STATE_NORMAL)
            {
                if (e.Insert.PropertyUpdated.Contains("ProductionId") ||
                    e.Insert.PropertyUpdated.Contains("DeliveryDetailId") ||
                    e.Insert.PropertyUpdated.Contains("ServiceId"))
                {
                    e.Message = "已审核的表单不能修改";
                    e.Cancle = true;
                    return;
                }

            }
            if(e.Insert.State==Definition.STATE_NORMAL)
            {
                if (e.Insert.AutoId > 0)
                {
                    ServiceBonusFetcher serviceBonusFetcher = new ServiceBonusFetcher();
                    if (serviceBonusFetcher.ListByServiceOrderId(e.Insert.Site, e.Insert.ServiceId, out message, out rows).Count > 0)
                    {
                        e.Message = "不允许有下游单据";
                        e.Cancle = true;
                        return;
                    }
                }
            }
        }
        private void DeliveryDetailValid(object sender, DataValidationEventArgs<ServiceOrderDetailModel> e)
        {
            string message;
            DeliveryDetailFetcher deliveryDetailFetcher = new DeliveryDetailFetcher();
            DeliveryDetailModel deliveryDetailModel = deliveryDetailFetcher.Get(e.Insert.Site, e.Insert.DeliveryDetailId, out message);
            if(deliveryDetailModel==null||deliveryDetailModel.State==Definition.STATE_NORMAL||deliveryDetailModel.State == Definition.STATE_READY)
            {
                e.Message = "上游物流信息不存在或者状态不正确";
                e.Cancle = true;
            }
        }
        private void ProductionValid(object sender, DataValidationEventArgs<ServiceOrderDetailModel> e)
        {
            string message;
            ProductionFetcher productionFetcher = new ProductionFetcher();
            if(productionFetcher.Get(e.Insert.Site,e.Insert.ProductionId,out message)==null)
            {
                e.Message = "产品档案不存在";
                e.Cancle = true;
            }
        }

    }
}
